package com.bos.prop.controller;

import com.bos.dao.*;
import com.bos.domain.*;
import com.bos.editor.*;
import com.bos.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class SekolahController {

    private SekolahDAO sekolahDAO;
    private KabupatenDAO kabupatenDAO;
    private KecamatanDAO kecamatanDAO;
    private BankDAO bankDAO;
    private KelurahanDAO kelurahanDAO;
	private RestTemplate restTemplate;
    /*private Credentials credentials;*/


    /* --------------------- Getter and Setter ---------------------*/

    /*public Credentials getCredentials() {
        return credentials;
    }

    @Autowired
    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
        if (restTemplate != null) {
            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
            HttpClient client = factory.getHttpClient();
            client.getState().setCredentials(AuthScope.ANY, credentials);
        }
    }*/

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

//        if (credentials != null) {
//            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
//            HttpClient client = factory.getHttpClient();
//            client.getState().setCredentials(AuthScope.ANY, credentials);
//        }
    }

    @Autowired
    public void setSekolahDAO(SekolahDAO sekolahDAO) {
        this.sekolahDAO = sekolahDAO;
    }

    @Autowired
    public void setBankDAO(BankDAO bankDAO) {
        this.bankDAO = bankDAO;
    }

    @Autowired
    public void setKelurahanDAO(KelurahanDAO kelurahanDAO) {
        this.kelurahanDAO = kelurahanDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    @Autowired
    public void setKecamatanDAO(KecamatanDAO kecamatanDAO) {
        this.kecamatanDAO = kecamatanDAO;
    }
/* -------------------------- Other Methods -------------------------- */

    @RequestMapping(value = "kabupaten/{kabupatenId}/sekolah", method = RequestMethod.GET)
    public String indexByKabupaten(ModelMap modelMap, @PathVariable("kabupatenId") Kabupaten kabupaten) {
        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());

        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(kabupaten) + "sekolah.json";
        try {
            ResponseEntity<List> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.GET, null, List.class);
            List<Sekolah> response = (List<Sekolah>) responseEntity.getBody();
            modelMap.addAttribute("data", response);
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }
        //atribute jika terjadi error saat melakukan pengambilan data dari host kabupaten
        if (!restError.isEmpty())
            modelMap.addAttribute("restError", restError.trim());

        modelMap.addAttribute("kabupaten", kabupaten);

        return "sekolah/index";
    }

    @RequestMapping(value = "kabupaten/{kabupatenId}/sekolah/{npsn}", method = RequestMethod.GET)
    public String show(ModelMap modelMap, @PathVariable("kabupatenId") Kabupaten kabupaten, @PathVariable("npsn") String npsn) {
        //Siapkan data baru yang akan dikirim ke host propinsi
        String restError = "";

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(kabupaten) + "sekolah/{npsn}.json";
        try {
            Map<String, Object> urlVariable = new HashMap<String, Object>();
            urlVariable.put("npsn",npsn);
            ResponseEntity<Sekolah> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.GET, null, Sekolah.class, urlVariable);
            Sekolah response = (Sekolah) responseEntity.getBody();
            modelMap.addAttribute("data", response);
        } catch (Exception e) {
            e.printStackTrace();
            restError += hostUrl + "\n";
        }
        //atribute jika terjadi error saat melakukan pengambilan data dari host kabupaten
        if (!restError.isEmpty())
            modelMap.addAttribute("restError", restError.trim());

        modelMap.addAttribute("kabMenuList", kabupatenDAO.getAll());
        modelMap.addAttribute("kabupaten", kabupaten);
        modelMap.addAttribute("tab", "sekolah");

        return "sekolah/show";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Bank.class, new BankEditor(bankDAO));
        binder.registerCustomEditor(Kelurahan.class, new KelurahanEditor(kelurahanDAO));
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(Kecamatan.class, new KecamatanEditor(kecamatanDAO));
        binder.registerCustomEditor(Sekolah.class, new SekolahEditor(sekolahDAO));
    }

}
